home *** CD-ROM | disk | FTP | other *** search
- ;unsigned short search_right(strg,sub_strg,start_pt);
- ; char *strg,*sub_strg;
- ; unsigned short start_pt;
-
- EXTRN _memory_model:byte
- EXTRN _error_code:byte
-
- _TEXT SEGMENT BYTE PUBLIC 'CODE'
- ASSUME CS:_TEXT
- PUBLIC _search_right
- _search_right proc near
- push bp ;
- mov bp,sp ;set up stack frame
- push di ;
- push si ;
- push ds ;
- cmp _memory_model,0 ;near or far?
- jle begin ;jump if near
- inc bp ;else add 2 to BP
- inc bp ;
- begin: cmp _memory_model,2 ;data near or far?
- jb L0 ;jump if near
- les di,dword ptr[bp+4] ;ES:DI pts to Strg
- lds si,dword ptr[bp+8] ;DS:SI pts to Substrg
- add bp,4 ;adjust stack ptr
- jmp short L00 ;jump ahead
- L0: mov ax,ds ;near case:
- mov es,ax ;
- mov di,[bp+4] ;
- mov si,[bp+6] ;
- L00: mov bx,[bp+8] ;startpoint in BX
- inc bx ;count from 1
- push di ;calculate string length
- mov cx,0ffffh ;counter
- sub al,al ;look for ASCII 0
- cld ;direction forward
- repne scasb ;seek end
- not cx ;reverse and adjust
- dec cx ;length now in CX
- pop di ;restore Strg ptr
- mov bp,2 ;2 = Strg null
- jcxz L5 ;quit if null
- push si ;get length of substring
- sub ah,ah ;
- L1: cmp byte ptr [si],0 ;
- je L2 ;
- inc ah ;
- inc si ;
- jmp short L1 ;
- L2: pop si ;
- inc bp ;3 = Substrg null
- or ah,ah ;test substring length
- jz L5 ;quit if null
- dec bp ;2 = Startpt out of range
- cmp bl,cl ;compare startpt to Strg length
- ja L5 ;quit if out of range
- dec bp ;1 = no match found
- sub cx,bx ;string length-startpoint
- inc cx ;adjust
- add di,cx ;set DI to startpoint
- dec di ;adjust
- inc cx ;1 extra for SCAS
- mov al,[si] ;use char for SCAS below
- inc si ;start SCAS from 2nd char
- L3: std ;direction flag backward
- repne scasb ;search the 1st char
- jcxz L5 ;jump ahead if not found
- push cx ;save num chars to scan
- mov cl,ah ;substring len in CX
- push si ;save string pointer
- push di ;save substring pointer
- inc di ;changing direction...
- inc di ;...so adjust DI
- cld ;change direction flag
- repe cmpsb ;compare the substring
- pop di ;restore string pointer
- pop si ;restore substring pointer
- jcxz L4 ;substring found if CX=0
- pop cx ;set CX for next scan
- jmp short L3 ;not matching, try again
- L4: pop cx ;since pushed before CMPS
- mov bp,0 ;no error
- jmp short L6 ;found! quit routine
- L5: mov cx,1 ;return 0 when not found (DECed)
- L6: mov ax,cx ;set result
- dec ax ;adjust
- pop ds ;restore DS
- mov bx,bp ;getch return code
- mov _error_code,bl ;set _error_code
- pop si ;
- pop di ;
- pop bp ;
- cmp _memory_model,0 ;quit
- jle quit ;
- db 0CBh ;RET far
- quit: ret ;RET near
- _search_right ENDP
- _TEXT ENDS
- END